package com.yootk.servlet;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Servlet跳转
 */
@WebServlet("/jump")
public class JumpServlet extends HttpServlet {

    @Override   // 进行GET请求的处理
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("request-msg", "www.yootk.com"); // request属性传递
        req.getSession().setAttribute("session-msg", "edu.yootk.com"); // session属性传递
        // 客户端跳转
        // resp.sendRedirect("/test02.jsp"); // 页面在根路径之中，所以可以直接跳转
        // 服务端跳转
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/test02.jsp"); // 具备了跳转的功能
        requestDispatcher.forward(req, resp); // 服务端跳转
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
